Completed
Push — development ( 53781d...dcaec7 )
by Nils
07:32
created

upgrade.js ➔ httpRequest   B

Complexity

Conditions 5
Paths 18

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 18
nop 3
dl 0
loc 52
rs 8.6868
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
D upgrade.js ➔ ... ➔ xhrObject.onreadystatechange 0 26 10

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * @file 		  upgrade.js
3
 * @author        Nils Laumaillé
4
 * @version       2.1.27
5
 * @copyright     (c) 2009-2011 Nils Laumaillé
6
 * @licensing     GNU AFFERO GPL 3.0
7
 * @link          http://www.teampass.net
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 */
13
14
// Function - do a pause during javascript execution
15
function PauseInExecution(millis)
16
{
17
    var date = new Date();
18
    var curDate = null;
0 ignored issues
show
Unused Code introduced by
The assignment to curDate seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
19
20
    do {
21
        curDate = new Date();
22
    } while(curDate-date < millis);
23
}
24
25
//Fonction qui permet d'appeler un fichier qui ex�cute une requete pass�e en parametre
26
function httpRequest(file,data,type) {
27
    var xhrObject = null;
28
    var isChrome = navigator.userAgent.toLowerCase().indexOf("chrome") > -1;
29
30
    if (document.getElementById("menu_action") !== null) {
31
        document.getElementById("menu_action").value = "action";
32
    }
33
34
    if(window.XMLHttpRequest) { // Firefox
35
        xhrObject = new XMLHttpRequest();
36
    } else if(window.ActiveXObject) { // Internet Explorer
37
        xhrObject = new ActiveXObject("Microsoft.XMLHTTP");  //Info IE8 now supports =>  xhrObject = new XMLHttpRequest()
38
    } else { // XMLHttpRequest non support? par le navigateur
39
        alert("Your browser does not support XMLHTTPRequest objects ...");
40
        return;
41
    }
42
43
    if (type === "GET") {
44
        xhrObject.open("GET", file+"?"+data, true);
45
        xhrObject.send(null);
46
    } else {
47
        xhrObject.open("POST", file, true);
48
        xhrObject.onreadystatechange = function() {
49
            if(xhrObject.readyState === 4) {
50
                eval(xhrObject.responseText);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
51
                //Check if query is for user identification. If yes, then reload page.
52
                if (data !== "" && data !== undefined && data.indexOf("ype=identify_user") > 0 ) {
53
                    if (isChrome === true ) {
54
                        // Needed pause for Chrome
55
                        PauseInExecution(100);
56
                    }
57
                    if (type === "") {
58
                        if (document.getElementById("erreur_connexion").style.display === "") {
59
                            //rise an error in url. This in order to display the eror after refreshing
60
                            window.location.href = "index.php?error=rised";
61
                        } else {
62
                            window.location.href = "index.php";
63
                        }
64
                    } else {
65
                        if (type === "?error=rised") {
66
                            if (document.getElementById("erreur_connexion").style.display === "none") type = "";   //clean error in url
67
                            else type = "?error=rised"; //Maintain the ERROR
68
                        }
69
                        window.location.href = "index.php"+type;
70
                    }
71
                }
72
            }
73
        }
74
        xhrObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
75
        xhrObject.send(data);
76
    }
77
}